// cl cat.cpp /std:c++20 /EHsc

#include <fstream>
#include <iostream>
#include <string>
#include <string_view>

using namespace std;

int main(int argc, char* argv[]) {
    if (argc < 2) {
        // no args provided
        cout << "Usage: " << argv[0] << " <path>\n";
        return 1;
    }

    ifstream f;
    f.open(argv[1], ios::in);
    if (!f.is_open()) {
        f.close();

        cout << "Failed to open " << argv[1] << "\n";
        return 1;
    }

    // 使用 std::string 并非必须，仅是一种方式，原 char array 仍然很好
    string buffer;
    buffer.resize(1024);
    const int cbMaxRead = buffer.length(); // always 1024

    while (!f.eof()) {
        f.read(buffer.data(), cbMaxRead);
        if (f.bad()) {
            cout << "I/O error\n";
            return 1;
        }

        auto cbRead = f.gcount();
        cout << string_view(buffer.data(), cbRead);
    }
    f.close();
    return 0;
}

// https://en.cppreference.com/w/cpp/io/basic_ifstream
// https://en.cppreference.com/w/cpp/string/basic_string
// https://en.cppreference.com/w/cpp/language/expressions#Operators for "operator <<"
